import pathToRegexp from 'path-to-regexp/index';
import {getEmerg,postEmerg,getEmergList,postEmergList,listDelete} from 'services/app';
import { routerRedux } from 'dva/router';

export default {
    namespace: 'emerg',
    state: {
        pages:1,
        record:{},
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //查看登录状态
        *getEmerg ({ payload }, { call, put }){
            const {data,total} = yield call(getEmerg, payload);
            yield put({ type: 'save', payload: {getEmergData:data,total:total,pages:payload.pageNumber}});
        },
        //添加物资信息
        *postEmerg ({ payload }, { call, put }){
            const data = yield call(postEmerg, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0,view:false}});
            }
        },
        //查看详情
        *goList ({ payload }, { call, put }){
            const {data} = yield call(getEmergList, payload);
            yield put({ type: 'save', payload: {getEmergListData:data}});
        },
        //添加详情
        *postList ({ payload }, { call, put }){
            const data = yield call(postEmergList, payload);
            if(data.code == 0){
                const {data} = yield call(getEmergList, {id:localStorage.getItem('id')});
                yield put({ type: 'save', payload: {getEmergListData:data,visible:false}});
            }
        },
        //删除详情
        *listDelete ({ payload }, { call, put }){
            const data = yield call(listDelete, payload);
            if(data.code == 0){
                const {data} = yield call(getEmergList, {id:localStorage.getItem('id')});
                yield put({ type: 'save', payload: {getEmergListData:data,visible:false}});
            }
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
